/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.client;

import org.apache.log4j.Logger;

import java.util.*;

public class BrokerList {
    private static final Logger logger = Logger.getLogger(BrokerList.class);
    private static ArrayList<String> brokerList = new ArrayList<String>();

    public static void shuffleBrokerList() {
        Collections.shuffle(brokerList);
        logger.info("Top broker after shuffling is" + brokerList.get(0));

    }

    public static String getTopBroker() {
        String brokerAddress = brokerList.get(0);
        return brokerAddress;
    }

    public static void addBrokerToList(String broker) {
        brokerList.add(broker);
        int size = brokerList.size();
        logger.info("Size of the broker list in client: " + size);
    }

    public static void removeBrokerFromList(String brokerAddress) {
        Iterator<String> it = brokerList.iterator();
        while (it.hasNext()) {
            String currentBroker = it.next();
            if (currentBroker.equals(brokerAddress)) {
                brokerList.remove(brokerAddress);

            }
        }
    }

    public static boolean isEmpty() {
        boolean empty = false;
        empty = brokerList.isEmpty();
        return empty;
    }

    public static void clearBrokerList() {
        brokerList.clear();
    }

}
